﻿using System;
using System.Data.OleDb;
using System.Collections;
using Negocio;
using System.Data.SqlClient;

public class ConnectionDB
{
    private static ConnectionDB instance;
    private OleDbConnection conexReal;
    private OleDbDataReader dr;

    private ConnectionDB(String cadena)
    {

        this.conexReal = new OleDbConnection(cadena);
    }

    public void connect()
    {               
        this.conexReal.Close();
        this.conexReal.Open();
        
    }
    static public ConnectionDB getInstance(String cadena)
    {
        if (instance == null)
        {
            instance = new ConnectionDB(cadena);
        }
        return instance;
    }

    static public ConnectionDB getInstance()
    {
        if (instance == null)
        {
            instance = new ConnectionDB("");
        }
        return instance;
    }

    public void disconnect()
    {
        this.conexReal.Close();
    }

    public void execute(string sentence)
    {
        OleDbCommand cmd = new OleDbCommand(sentence, this.conexReal);
        cmd.ExecuteNonQuery();
    }

    public void bringData(string sentence)
    {
        OleDbCommand cmd = new OleDbCommand(sentence, this.conexReal);
        this.dr = cmd.ExecuteReader();
    }

    public void bringList(string sentence)
    {
        OleDbCommand cmd = new OleDbCommand(sentence, this.conexReal);
        this.dr = cmd.ExecuteReader();
    }

    public int bringOID()
    {
        int OID = 0;
        this.connect();
        string sentencia = "Select * from OID";
        OleDbCommand cmd = new OleDbCommand(sentencia, this.conexReal);
        this.dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            OID = (int)dr["OId"];
        }
        this.disconnect();
        return OID;
    }

    public bool nextRecord()
    {
        bool hayRegistro = false;
        if (this.dr.Read())
        {
            hayRegistro = true;
        }
        return hayRegistro;
    }

    public object getDataString(string cadena)
    {
        return dr[cadena];
    }

    public object getDataIndex(int indice)
    {
        return dr[indice];
    }
    public string textoConexion()
    {
        return this.conexReal.ToString();
    }
    public OleDbConnection conexionReal
    {
        get { return conexReal;}
    }

    
}